/**
 * Created by CZ on 2017/4/6.
 */
module.exports = function ($scope,$rootScope,$state,modelService,RequestService,$localStorage,commonConfig,$modal) {
    $rootScope.isShowFooter = false;
    //全局定义所有图片初始路径(避免未经首页而是刷新导致不识别imgApi)
    $rootScope.imgApi = commonConfig.$webroot+commonConfig.imgApi+'?access_token='+$localStorage.get('cloud').token+'&uuid=';

    $scope.logoImg = commonConfig.$webroot+commonConfig.imgApi+'?access_token='+$localStorage.get('cloud').token+'&uuid=632caf5cb71545f6a8a9fff8ca9658c2'
    $rootScope.User = $localStorage.get('cloud');
    if($rootScope.User === undefined || $rootScope.User === null || $rootScope.User.username === ''){  //避免用户直接敲路径跳转
        window.location.href = commonConfig.tokenApi+'login?logout';
    }
    $scope.Exit = function (){
        modelService.openExitModel('确定要退出么？','CloudPlatform.home','cloud')
    };
    getSchool();
    // checkAuthority();
    //获取学校数据
    function getSchool(){
        RequestService.Request(commonConfig.cloudApi+'cloud/tenant/'+$rootScope.User.detail.tenantId,'GET').then(function(response){
            console.log(response);
            //存储学校信息
            $rootScope.User.school = response;
            $scope.School = response;
            // $rootScope.UserMsgIsSave = true;
            getUserExtendInfo();
        },function(error){
            console.log(error);
        });
    }
    function getUserExtendInfo(){
        RequestService.Request(commonConfig.userApi+'user/user/getUserExtendById?userId=' + $rootScope.User.detail.id, 'GET').then(function(response){
            console.log(response);
            $rootScope.UserExtendInfo = response;
            $rootScope.User.UserExtendInfo = response;
            $localStorage.set('cloud',$rootScope.User);
        }, function(error){
            console.log(error);
        });
    }
    //判断是否为学校管理员
    // function checkAuthority(){
    //     if($rootScope.User.authority === 'SchoolAdministrator'){
    //         $rootScope.isSchoolManager = true;
    //     }else {
    //         $rootScope.isSchoolManager = false;
    //     }
    // }
    //重置密码方法
    $scope.resetPsw = function(){
        var modalInstance = $modal.open({
            templateUrl : './views/cloud/CloudUser/ResetPsw.html',
            size : "md",
            keyboard:false,
            backdrop:"static",
            controller : function($scope, $modalInstance){
                $scope.form = {
                    newPassword: "",
                    renewPassword:"",
                    oldPassword: "",
                    userId: $rootScope.User.detail.id
                };
                $scope.ok = function(){
                    delete $scope.form.renewPassword;
                    RequestService.Request(commonConfig.userApi+'user/user/updatePassword','POST',$scope.form).then(function(res){
                        console.log(res);
                        if (res.code == 0) {
                            $modalInstance.close();
                        }else {
                            $modalInstance.dismiss(res.message);
                        }
                    },function(error){
                        $modalInstance.dismiss();
                        console.log(err)
                    });
                };
                $scope.cancel = function(){
                    $modalInstance.dismiss();
                };

            },
            scope : $scope //联系作用域
        });
        modalInstance.result.then(function(res){ //	一个promise，窗口关闭时为resolved，窗口取消时为rejected
            //$modalInstance.close();回调的方法
            $modal.open({
                animation: true,
                templateUrl: './views/model/AlertModel.html',
                size: 'md',
                keyboard:false,
                backdrop:"static",
                controller: function($scope,$modalInstance) {
                    $scope.content = '密码修改成功,请重新登录！';
                    $scope.ok = function () {
                        $modalInstance.close();
                        $localStorage.remove('cloud');
                        window.location.href = commonConfig.tokenApi+'login?logout';
                    };

                }
            });
        }, function(msg){
            if(msg){
                modelService.openTipsModel('密码修改失败:'+msg);
            }
            //$modalInstance.dismiss();回调的方法
        });
    };
    //检测是否需要修改密码
    if($rootScope.User.detail.warningPassword){
        $scope.resetPsw()
    }


};